import os.path
import sys
from unittest import TestCase
import tempfile
import shutil

from mercurial.ui import ui
from mercurial.hg import repository

path = os.path.abspath(__file__)
path = os.path.dirname(path)
path = os.path.join(path, '..')
path = os.path.normpath(path)
sys.path.append(path)

import ppatches
from ppatches import BASE_DIR
from ppatches import PATCH_DIR
from ppatches import RevMap


BASE_PATH = os.path.dirname(__file__)
DATA_DIR = 'data'


class PPatchesTestCase(TestCase):

    def setUp(self):
        dest = tempfile.mktemp()
        self.repo = repository(ui(), dest, create=True)
        self.repo.ui.quiet = True
        os.makedirs(os.path.join(self.repo.path, BASE_DIR, PATCH_DIR))

        dst = os.path.join(self.repo.path, 'hgrc')
        filepath = ppatches.__file__
        filepath = filepath.replace('.pyc', '.py')

        with open(dst, 'w') as fp:
            fp.write('[extensions]\nppatches = %s' % filepath)

    def tearDown(self):
        shutil.rmtree(self.repo.root)

    def get_patch_src(self, patch):
        return os.path.join(BASE_PATH, DATA_DIR, patch)

    def get_patch_dst(self, patch):
        return os.path.join(self.repo.path, BASE_DIR, PATCH_DIR, patch)


